﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Linq;
using BemEstarEmprego.BLL;
using BemEstarEmprego.BLL.Models;
using BemEstarEmprego.Entities;
//using BemEstarEmprego.Web.Models;
using BemEstarEmprego.Web.Helpers;

namespace BemEstarEmprego.Web.Controllers
{
	[SecurityFilter]
	public class CandidatoController : BaseController
	{
		// GET: /Candidato/

		public ActionResult Index()
		{
			return View();
		}

		public ActionResult Cadastro()
		{
			ViewBag.Action = "Salvar";
			return View();
		}


		[HttpPost]
		[MultiButton(MatchFormKey = "action", MatchFormValue = "Salvar")]
		public ActionResult Salvar(CandidatoModel model, EnderecoModel modelEndereco)
		{
			CandidatoBLL candidatoBLL = new CandidatoBLL();
			EnderecoBLL enderecoBLL = new EnderecoBLL();

			model._pessoa.endereco = modelEndereco._endereco;
			var retornoSave = candidatoBLL.Save(model._pessoa);

			return RedirectToAction("list");
		}

		[HttpPost]
		[MultiButton(MatchFormKey = "action", MatchFormValue = "Voltar")]
		public ActionResult Voltar()
		{
			return RedirectToAction("list");
		}

		public ActionResult Editar(String id)
		{
			Int32 _id = Convert.ToInt32(id);
			CandidatoModel model = new CandidatoModel();

			ViewBag.Action = "Alterar";

			using (bemestar_empregoEntities context = new bemestar_empregoEntities())
			{

				var pessoa = context.pessoa.Where(x => x.ID == _id).FirstOrDefault<pessoa>();
				pessoa.enderecoReference.Load();
				pessoa.usuarioReference.Load();

				model._pessoa = pessoa;

				if (pessoa.endereco != null)
				{
					model.enderecoModel = new EnderecoModel();
					model.enderecoModel._endereco = pessoa.endereco;
				}

				return View(model);
			}
		}

		[HttpPost]
		[MultiButton(MatchFormKey = "action", MatchFormValue = "Alterar")]
		public ActionResult Alterar(CandidatoModel model)
		{
			CandidatoBLL candidato = new CandidatoBLL();
			candidato.UpdateCandidato(model._pessoa);

			EnderecoBLL endereco = new EnderecoBLL();
			endereco.UpdateEndereco(model._endereco);

			return RedirectToAction("list");
		}

		public ActionResult List()
		{
			string pageString = "";

			try
			{
				pageString = Request.Url.Segments[3];
			}
			catch (Exception)
			{
				pageString = null;
			}

			int page = (String.IsNullOrEmpty(pageString)) ? 1 : Int32.Parse(pageString);

			int totalCandidato = new CandidatoModel().GetTotalCandidatos();

			Extension.Pagination pagination = new Extension.Pagination(true);

			pagination.BaseUrl = "/candidato/List/";
			pagination.TotalRows = totalCandidato;
			pagination.CurPage = page;
			pagination.PerPage = 10;

			pagination.PrevLink = "Prev";
			pagination.NextLink = "Next";

			string pageLinks = pagination.GetPageLinks();

			int start = (page - 1) * pagination.PerPage;
			int offset = pagination.PerPage;

			List<pessoa> candidatos = new CandidatoModel().GetCandidatos(start, offset);

			ViewData["title"] = "Pagination in Asp.Net Mvc";
			ViewData["totalCandidato"] = totalCandidato;
			ViewData["empresas"] = candidatos;
			ViewData["pageLinks"] = pageLinks;
			ViewBag.Links = pageLinks;

			return View("Consulta");
		}

	}
}
